package com.education.platform.management.service.impl;

import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.dao.CoursewareDao;
import com.education.platform.dao.entity.CoursewareDO;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.service.CoursewareService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
* describe: 课件
* @author : tomato
* @date: 2020-06-29 10:28:07.450
**/
@Service
public class CoursewareServiceImpl extends BaseServiceImpl<CoursewareDao, CoursewareDO> implements CoursewareService {

	private static final Logger LOGGER = LoggerFactory.getLogger(CoursewareServiceImpl.class);

    @Autowired
	private CoursewareDao coursewareDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CoursewareDO courseware) {
        if(courseware == null){
            throw new BusinessException("数据为空");
        }
        if(courseware.getId()==null){
            courseware.setCreateTime(new Date());
            courseware.setCreateBy(ThreadUser.getUserName());
            courseware.setTeacherId(ThreadUser.getUserId());
            courseware.setTeacherName(ThreadUser.getUserName());
            if(coursewareDao.insert(courseware)!=1){
                throw new BusinessException("保存失败");
            }
        }else{
            courseware.setUpdateTime(new Date());
            courseware.setUpdateBy(ThreadUser.getUserName());
            if(coursewareDao.updateById(courseware)!=1){
                throw new BusinessException("保存失败");
            }
        }
    }
}
